package com.ecoolex.chinadutyfree.retailer.job.manager;

import com.ecoolex.chinadutyfree.retailer.core.manager.DirectMailGoodsOrderChannelManager;
import com.ecoolex.chinadutyfree.retailer.core.manager.DirectMailGoodsOrderCouponManager;
import com.ecoolex.chinadutyfree.retailer.core.manager.DirectMailGoodsOrderDetailManager;
import com.ecoolex.chinadutyfree.retailer.core.manager.DirectMailGoodsOrderManager;
import com.ecoolex.chinadutyfree.retailer.core.manager.StoreManager;
import com.ecoolex.chinadutyfree.retailer.core.manager.UserExternalInfoManager;
import com.ecoolex.chinadutyfree.retailer.entity.DirectMailGoodsOrder;
import com.ecoolex.chinadutyfree.retailer.entity.DirectMailGoodsOrderChannel;
import com.ecoolex.chinadutyfree.retailer.entity.DirectMailGoodsOrderCoupon;
import com.ecoolex.chinadutyfree.retailer.entity.DirectMailGoodsOrderDetail;
import com.ecoolex.chinadutyfree.retailer.entity.Store;
import com.ecoolex.chinadutyfree.retailer.entity.UserExternalInfo;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.union.pay.retailer.PushOrderDTO;
import com.ecoolex.union.pay.retailer.pojo.PushOrderChannelDTO;
import com.ecoolex.union.pay.retailer.pojo.PushOrderCouponDTO;
import com.ecoolex.union.pay.retailer.pojo.PushOrderDetailDTO;
import com.ecoolex.union.pay.retailer.ws.UnionPayOrderPushService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class OrderPushManager {

    @Reference
    private UnionPayOrderPushService unionPayOrderPushService;

    @Autowired
    private UserExternalInfoManager userExternalInfoManager;

    @Autowired
    private DirectMailGoodsOrderCouponManager directMailGoodsOrderCouponManager;

    @Autowired
    private DirectMailGoodsOrderChannelManager directMailGoodsOrderChannelManager;

    @Autowired
    private DirectMailGoodsOrderDetailManager goodsOrderDetailManager;

    @Autowired
    private StoreManager storeManager;

    @Autowired
    private DirectMailGoodsOrderManager directMailGoodsOrderManager;

    public void push(Integer orderId){
        DirectMailGoodsOrder order = directMailGoodsOrderManager.getById(orderId);
        UserExternalInfo userExternalInfo = userExternalInfoManager.getByUserId(order.getUserId());
        PushOrderDTO pushOrderDTO = new PushOrderDTO();
        pushOrderDTO.setOrderId(order.getOrderId());
        pushOrderDTO.setOrderNo(order.getOrderNo());
        pushOrderDTO.setStatus(order.getStatus());
        pushOrderDTO.setTotalPrice(order.getTotalPrice());
        pushOrderDTO.setUserId(order.getUserId());
        if(Check.notNull(userExternalInfo) && Check.notEmpty(userExternalInfo.getExternalUserId())){
            pushOrderDTO.setExternalUserId(userExternalInfo.getExternalUserId());
        }
        pushOrderDTO.setCurrencyType(order.getCurrencyType());
        pushOrderDTO.setCreateTime(order.getCreateTime());
        pushOrderDTO.setUpdateTime(order.getUpdateTime());
        pushOrderDTO.setRealPrice(order.getRealPrice());
        if(Check.notNull(order.getPaymentTime())) {
            pushOrderDTO.setPaymentTime(order.getPaymentTime());
        }

        //查订单使用的优惠券
        List<PushOrderCouponDTO> couponDTOList = new ArrayList<>();
        List<DirectMailGoodsOrderCoupon> orderCouponList = directMailGoodsOrderCouponManager.queryByOrderId(order.getOrderId());
        for (DirectMailGoodsOrderCoupon orderCoupon : orderCouponList) {
            PushOrderCouponDTO pushOrderCouponDTO = new PushOrderCouponDTO();
            pushOrderCouponDTO.setCouponId(orderCoupon.getCouponId());
            pushOrderCouponDTO.setCouponType(orderCoupon.getCouponType());
            couponDTOList.add(pushOrderCouponDTO);
        }

        //查订单明细
        List<PushOrderDetailDTO> detailDTOList = new ArrayList<>();
        List<DirectMailGoodsOrderDetail> detailList = goodsOrderDetailManager.queryByOrderId(order.getOrderId());
        for (DirectMailGoodsOrderDetail detail : detailList) {
            PushOrderDetailDTO orderDetailDTO = new PushOrderDetailDTO();
            orderDetailDTO.setDirectMailGoodsId(detail.getDirectMailGoodsId());
            orderDetailDTO.setName(detail.getName());
            orderDetailDTO.setSpecNames(detail.getSpecNames());
            orderDetailDTO.setRetailPrice(detail.getRetailPrice());
            orderDetailDTO.setBuyNumber(detail.getBuyNumber());
            detailDTOList.add(orderDetailDTO);
        }
        //查渠道信息
        DirectMailGoodsOrderChannel orderChannel = directMailGoodsOrderChannelManager.getChannelByOrderId(order.getOrderId());
        PushOrderChannelDTO pushOrderChannelDTO = new PushOrderChannelDTO();
        if(Check.notNull(orderChannel)){
            pushOrderChannelDTO.setChannelId(orderChannel.getChannelId());
        }
        Pagination<Store> pagination = storeManager.queryStore(new Page());
        if(Check.notNull(pagination) && Check.notNull(pagination.getList()) && !pagination.getList().isEmpty()){
            Store store = pagination.getList().get(0);
            pushOrderDTO.setServiceId(store.getServiceId());
            pushOrderDTO.setOrderUrl(store.getOrderDetailUrl());
            pushOrderChannelDTO.setMerchantId(store.getStoreId());
            pushOrderChannelDTO.setMerchantName(store.getName());
        }

        unionPayOrderPushService.push(pushOrderDTO,couponDTOList,detailDTOList,pushOrderChannelDTO);
    }
}
